﻿
       var dejianshu=0;       //得奖人数
       var level = 3;
       var level_old = 0;
       var index=0,           //当前亮区位置
       prevIndex=0,          //前一位置
       Speed=300,           //初始速度
       Time,            //定义对象
       arr = [],         //初始化数组
	   zjarr = [],
         EndIndex=0,           //决定在哪一格变慢
         ptb,     //获取tb对象 
         cycle=0,           //转动圈数   
         EndCycle=0,           //计算圈数
        flag=false,           //开始转动标志 
        quick=0,           //加速
		maxnum=0,      // 最大开奖号码
        btnplays;
     
     /*
      * 删除左右两端的空格
      */
     function Trim(str){
         return str.replace(/(^\s*)|(\s*$)/g, ""); 
     }
     
        /*
         * 定义数组
         */
        function GetSide(m,n){
            //初始化数组
            var arr = [];
            for(var i=0;i<m;i++){
                arr.push([]);
                for(var j=0;j<n;j++){
                    arr[i][j]=i*n+j;
                }
            }
            //获取数组最外圈
            var resultArr=[];
            var tempX=0,
             tempY=0,
             direction="Along",
             count=0;
            while(tempX>=0 && tempX<n && tempY>=0 && tempY<m && count<m*n)
            {
                count++;
                resultArr.push([tempY,tempX]);
                if(direction=="Along"){
                    if(tempX==n-1)
                        tempY++;
                    else
                        tempX++;
                    if(tempX==n-1&&tempY==m-1)
                        direction="Inverse";
                }
                else{
                    if(tempX==0)
                        tempY--;
                    else
                        tempX--;
                    if(tempX==0&&tempY==0)
                        break;
                }
            }
            return resultArr;
        }
        
       
        
        function StartGame(){
            autoCloseWindow();
			maxnum = $("#maxnumst").val();
			if(maxnum == "")
			{
			alert("请设置参与抽奖的最大号码");
			$("#maxnumst").addClass("playcurr");
			return;
			}
			maxnum = maxnum.trim();
			var reg = new RegExp("^[0123456789]+$");
			if (!reg.test(maxnum) || maxnum < 1)
			{
			alert("请正确设置参与抽奖的最大号码");
			$("#maxnumst").addClass("playcurr");
			return;
			}
		 ptb = document.getElementById("ptb");
         btnplays = $("#btnplays");
		

		 $("#maxnumst").attr("disabled",true);
		 arr = GetSide(5,5);

	     clearInterval(Time);
         cycle=0;
        //  flag=true;
         EndIndex=Math.floor(Math.random()*16);
         
         EndCycle=1;
         btnplays.val("抽奖中...");
		 btnplays.attr("disabled",true);
		 Speed = 300;
         Time = setInterval(Star,Speed);
        }
        
		//跑马灯变速
        function Star(){
            
              //走五格开始加速
             if(quick==15){
                 clearInterval(Time);
                 Speed=20;
				 flag=true;
                 Time=setInterval(Star,Speed);
             }
          
            
            if(index>=arr.length){
                index=0;
                cycle++;
            }
		 
            ptb.rows[arr[index][0]].cells[arr[index][1]].className="playcurr";
            if(index>0){
                prevIndex=index-1;
            }else{
                prevIndex=arr.length-1;
            }
            ptb.rows[arr[prevIndex][0]].cells[arr[prevIndex][1]].className="playnormal";
            index++;
            quick++;
            
        }

//结束转动并选中号码
		function choujiang(){

		   if(flag==false)
		   {
				return;
		   }
		  flag = false;
		  quick=0;
		  cycle=0;
		  clearInterval(Time);
		  btnplays.attr("disabled",false);
		  btnplays.val("开始抽奖");
		   dejianshu=dejianshu+1;

		  var zjnum = getZJnumber(maxnum)

         $("#zhongjiang").text("恭喜 "+zjnum+"号中奖");
         popCenterWindow();
         var imgpath = "images/jinyuanbao.jpg"
		  $("#xingyun").attr("src",imgpath);
            //level发生变更
            if(level!=level_old){
                level_old = level;
                if(level==3){
                    $("#yizj").append("<br><span style='font-size: 88px;background-color:orange;color:#FFFFFF;'>三等奖:</span>");
                }
                if(level==2){
                        $("#yizj").append("<br><span style='font-size: 88px;background-color:orange;color:#FFFFFF;'>二等奖:</span>");
                }
                if(level==1){
                        $("#yizj").append("<br><span style='font-size: 88px;background-color:orange;color:#FFFFFF;'>一等奖:</span>");
                }
                if(level==0){
                        $("#yizj").append("<br><span style='font-size: 88px;background-color:orange;color:#FFFFFF;'>特等奖:</span>");
                }

            }
                 
		  $("#yizj").append("<span style='font-size: 88px;background-color:orange;color:#FFFFFF;'>"+ zjnum + ",</span>");
		// var yizinumb = $("#yizj").innerHTML();
		 //yizinumb = yizinumb + zjnum + ",";
		// $("#yizj").innerHTML(yizinumb);
		}

        // 中奖的随机数
		function getZJnumber(maxnum)
		{
	     maxnum = parseInt(maxnum);
		 var zjnum = 0;
		 while(true)
		  { 
		   zjnum = Math.floor(Math.random()*maxnum*4 - maxnum)
		   if( 0< zjnum && zjnum < maxnum + 1 && $.inArray(zjnum,zjarr) == -1) 
			  {
			   zjarr.push(zjnum);
			   break;
			  }
		  }
			
		return zjnum;
        }
        
        function change(lev){
            level = lev;
        }